package com.tongzhou.client;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Test {
	public static void main(String[] args) {
		
		
		try {
			String cont = URLEncoder.encode(new String("你在干什么啊"), "utf-8");
			System.out.println("0："+cont);            
	        String after=URLDecoder.decode(cont, "utf-8"); 
	        System.out.println("1："+after);
	        
	        String name="中国";
	        name=URLEncoder.encode(new String(name), "utf-8");
			String url="http://localhost:8080/TongZhou/client/tongzhou.do?method=1&json={'companyName':'"+name+"','companyNum':'abc001'}";
			System.out.println(url);
//			String url="http://www.taobao.com";
			System.out.println(GetNetData(url,1));
//			String name="好人".getBytes("gbk").toString();
//			System.out.println(name);
//			String key=new String(name.getBytes("ISO-8859-1"),"UTF-8");
//			System.out.println(key);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//		URL urls 						= null;
//		HttpURLConnection connection 	= null;
////		companyName="佛山市南海中艺联发彩印包装厂";
////		company_num="ZYLF01";
////		String url="http://www.tongzhoudg.com/tongzhouClient/client.do";
////		String url="http://www.tongzhoudg.com/tongzhouClient/client.do?method=1&json={'companyName':'abc','companyNum':'abc001'}";
//		String url="http://localhost:8080/TongZhou/client/tongzhou.do";
//		//获取网络数据
//		try
//		{
//			urls = new URL(url);
//
//			
//			
//			connection = (HttpURLConnection) urls.openConnection();
//			
//			
//			connection.setRequestMethod("POST");  
//			connection.setDoOutput(true);  
//			connection.setDoInput(true);
//			
//			
//			
//			connection.setRequestProperty("accept", "text/xml;text/html");  
//			connection.setRequestProperty("Content-Type","text/xml;charset=utf-8");  
//			
//			connection.addRequestProperty("method","1");  
//			connection.addRequestProperty("json","{'companyName':'abc','companyNum':'abc001'}");
//			
//			connection.setRequestProperty("method", "1");
//			
//			
//			
//
//			
//			connection.setConnectTimeout(10 * 1000);//设置连接主机超时(单位：毫秒)
//			connection.setReadTimeout(10 * 1000);	//设置从主机读取数据超时(单位：毫秒)
//			
//			
//			
////			connection.getOutputStream().write(url.getBytes("UTF-8"));  
////			connection.getOutputStream().flush();  
////			connection.getOutputStream().close();  
//			
//			System.out.println(InputStreamtoString(connection.getInputStream()).trim());
////			return InputStreamtoString(connection.getInputStream()).trim();
//		}catch (Exception e) {
//			e.printStackTrace();
//		}
	}
	
	private static String GetNetData(String url, int flag)
	{
		//连接网络获取数据
		URL urls 						= null;
		HttpURLConnection connection 	= null;
		
		//获取网络数据
		try
		{
			urls = new URL(url);
			
			connection = (HttpURLConnection) urls.openConnection();
			connection.setConnectTimeout(10 * 1000);//设置连接主机超时(单位：毫秒)
			connection.setReadTimeout(10 * 1000);	//设置从主机读取数据超时(单位：毫秒)
				return InputStreamtoString(connection.getInputStream()).trim();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String InputStreamtoString(InputStream in) throws IOException 
	{
		StringBuffer out = new StringBuffer();
		
		byte[] buffer = new byte[1024];
		
		for (int i; (i = in.read(buffer)) != -1;) 
		{
			out.append(new String(buffer, 0, i));
		}
		
		return out.toString();
	}
}
